﻿Partial Public Class WFrm_Crear_Modificar_Pregunta
    Inherits Wfrm_Master


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            ViewState("secciones") = Session("secciones")
            ViewState("id_prueba") = Session("id_prueba")
            ViewState("nombre_prueba") = Session("nombre_prueba")
            ViewState("txt_codigo_prueba") = ViewState("txt_codigo_prueba")
            ViewState("txt_Observacion_prueba") = Session("txt_Observacion_prueba")
            'Cargar_Preguntas()
        End If
    End Sub
    Sub Cargar_Preguntas()
        'SP_SELECCIONA_PRUEBA_DISENNO_PREGUNTA
        Dim rp As New DotNetResponse.SQLPersistence
        Dim Exec As New DotNetDal.SQLPersistence(False, "cnn", DotNetDal.SQLPersistence.Provider.SQLCLIENT)
        rp.StoredProcedure = "SP_SELECCIONA_PRUEBA_DISENNO_PREGUNTA"
        rp.AddParametro(ViewState("id_prueba"))
        Exec.ExecuteDataTable(rp)


    End Sub
    
    Protected Sub Btn_Agregar_Pregunta_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Btn_Agregar_Pregunta.Click
        'SP_CREAR_PREGUNTA_DISENNO

        Dim myStream As System.IO.Stream
        Dim fileLen As Integer
        Dim displayString As New StringBuilder()
        Dim bytes() As Byte
        Dim URLIMAGEN As String = ""


        Dim chk_file As Boolean = False
        If Not FileUpload1.PostedFile Is Nothing Then
            If Not FileUpload1.PostedFile.FileName = "" Then
                chk_file = True

                fileLen = FileUpload1.PostedFile.ContentLength
                Dim Input(fileLen) As Byte
                bytes = Input

                myStream = FileUpload1.PostedFile.InputStream  ' M_IMAGEN_PREGUNTA.PostedFile.FileContent
                myStream.Read(Input, 0, fileLen)

                Dim stream As New System.IO.MemoryStream()

                Dim strTempPath As String = System.Configuration.ConfigurationManager.AppSettings("rutaimagen")
                Dim strFileName As String = Guid.NewGuid.ToString("N") + ".img"
                Dim fs As New IO.FileStream(strTempPath + "\" + strFileName, IO.FileMode.OpenOrCreate, IO.FileAccess.Write)
                Dim offset As Integer = 0
                fs.Write(Input, offset, Input.Length - offset)
                fs.Close()
                fs = Nothing

                URLIMAGEN = "./UploadImagenes/" & strFileName

                'Image1.ImageUrl = System.Configuration.ConfigurationManager.AppSettings("rutalogicaimagen") & strFileName
                'Image1.UpdateAfterCallBack = True
            End If
        End If


        'FileUpload1

        Dim rp As New DotNetResponse.SQLPersistence
        ' DalAppTestBuilder.TB_PRUEBA_DISEÑO_PREGUNTA.CREAR_PRUEBA_DISENNO_PREGUNTA(rp, Nothing, Val(lbl_id_pregunta.Text), cmb_seccion_pregunta.SelectedValue, txt_Observacion_prueba1.Text.Trim, bytes, chk_file.ToString, cmb_puntos_pregunta.Text, "V", 0, ViewState("usuario"), CMB_K_TIPO_PREGUNTA.SelectedValue, chk_file.ToString, "", URLIMAGEN)


        If Not rp.Errores Then

            MsgInformativo("Sección Eliminada")

            Cargar_Preguntas()



        Else
            MsgError("Imposible Eliminar Sección")

        End If


    End Sub

    Protected Sub img_eliminar_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs)
        Session("pregunta_id") = DirectCast(sender.parent.parent, System.Web.UI.WebControls.DataGridItem).Cells(0).Text

        MsgConfirm("¿Está Seguro(a) de Eliminar la Pregunta?", "function(r){ if(r){Anthem_InvokePageMethod('Eliminar_Pregunta',[" & Session("pregunta_id") & "],function(result) {});} }")

    End Sub
    <Anthem.Method()> _
  Public Function Eliminar_Seccion(ByVal Id_Seccion As String)

        If Session("seccion_id") Is Nothing Then
            Return Nothing
        End If

        Dim rp As New DotNetResponse.SQLPersistence
        ' DalAppTestBuilder.TB_PRUEBA_DISENO_SECCION.ELIMINA_PRUEBA_DISENNO_SECCION(rp, Nothing, Session("seccion_id"), ViewState("usuario"))


        If Not rp.Errores Then

            MsgInformativo("Sección Eliminada")

            'Cargar_Seccion()

            Return True

        Else
            MsgError("Imposible Eliminar Sección")
            Return False
        End If


    End Function

    Protected Sub lnk_editar_Click(ByVal sender As Object, ByVal e As EventArgs)

    End Sub
    Protected Sub Btn_Actualizar_Imagen_Click(ByVal sender As Object, ByVal e As EventArgs) ' Handles Btn_Actualizar_Imagen.Click
        Dim myStream As System.IO.Stream
        Dim fileLen As Integer
        Dim displayString As New StringBuilder()
        Dim bytes() As Byte
        Dim URLIMAGEN As String = ""

        If Not FileUpload1.PostedFile Is Nothing Then
            If Not FileUpload1.PostedFile.FileName = "" Then

                fileLen = FileUpload1.PostedFile.ContentLength
                Dim Input(fileLen) As Byte
                bytes = Input

                myStream = FileUpload1.PostedFile.InputStream  ' M_IMAGEN_PREGUNTA.PostedFile.FileContent
                myStream.Read(Input, 0, fileLen)

                Dim stream As New System.IO.MemoryStream()

                Dim strTempPath As String = System.Configuration.ConfigurationManager.AppSettings("rutaimagen")
                Dim strFileName As String = Guid.NewGuid.ToString("N") + ".img"
                Dim fs As New IO.FileStream(strTempPath + "\" + strFileName, IO.FileMode.OpenOrCreate, IO.FileAccess.Write)
                Dim offset As Integer = 0
                fs.Write(Input, offset, Input.Length - offset)
                fs.Close()
                fs = Nothing

                URLIMAGEN = "./UploadImagenes/" & strFileName



            End If
        End If
    End Sub
End Class